import {
  queryArticleItemAll,
  queryArticleAll
} from '../../services/article/article';

import {
  queryExamIds
} from '../../services/exam/exam';


import {
  baseUrl
} from '../../config/index'
import Toast from "tdesign-miniprogram/toast/index";

// pages/exam/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    examList: [],
    examIds: [],
    loadStatus: 0,
    currentPages: 1,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // 获取所有试卷
    const ids = await queryExamIds().then(res => {
      this.setData({
        examIds: res.data.data
      })
      return res.data.data;
    })
    console.log("考试所有ids");
    console.log(ids);
    this.getData(1)
  },
  async getData(current) {
    const ids=this.data.examIds;
    queryArticleAll({
      pageSize: 6,
      pageNum: current,
      type: 2
    }).then(res => {
      console.log(res.data);
      const data = res.data.data.records;
      data.forEach(item => {
        item.has = ids.includes(String(item.id))
        console.log(item.id);
        console.log(ids.includes(String(item.id)));
      })
      this.setData({
        currentPages: res.data.data.current,
        examList: [...this.data.examList, ...data],
        total: res.data.data.pages
      })
    }).catch(error => {
      console.log(error);
      Toast({
        context: this,
        selector: "#t-toast",
        message: error,
      });
    })
  },

  examClick(e) {
    // 点击了按钮
    console.log(e);
    const id = e.currentTarget.id
    wx.navigateTo({
      url: `/pages/exam/details/index?id=${id}`,
    });
  },
  examButtonClick(e) {
    // 点击了按钮
    console.log(e);
    const id = e.currentTarget.dataset.id
    // const id = this.data.examList[data].id;
    wx.navigateTo({
      url: `/pages/exam/details/index?id=${id}`,
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    console.log("触及底部");
    if ((this.data.currentPages + 1) > this.data.total) {
      Toast({
        context: this,
        selector: "#t-toast",
        message: "对不起，一共" + this.data.total + "页，已经到达底部",
      });
      return;
    }
    if (this.data.loadStatus === 0) {
      // 开始刷新数据
      this.setData({
        loadStatus: 1,
      });

      // 这里正在加载数据
      const data = await this.getData(this.data.currentPages + 1);
      this.setData({
        goodsListLoadStatus: 0,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})